package cn.iocoder.yudao.module.course.service.interactive;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.course.controller.admin.interactive.vo.*;
import cn.iocoder.yudao.module.course.dal.dataobject.interactive.InteractiveDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.course.dal.mysql.interactive.InteractiveMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.course.enums.ErrorCodeConstants.*;

/**
 * 课程交互 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class InteractiveServiceImpl implements InteractiveService {

    @Resource
    private InteractiveMapper interactiveMapper;

    @Override
    public Long createInteractive(InteractiveSaveReqVO createReqVO) {
        // 插入
        InteractiveDO interactive = BeanUtils.toBean(createReqVO, InteractiveDO.class);
        interactiveMapper.insert(interactive);
        // 返回
        return interactive.getId();
    }

    @Override
    public void updateInteractive(InteractiveSaveReqVO updateReqVO) {
        // 校验存在
        validateInteractiveExists(updateReqVO.getId());
        // 更新
        InteractiveDO updateObj = BeanUtils.toBean(updateReqVO, InteractiveDO.class);
        interactiveMapper.updateById(updateObj);
    }

    @Override
    public void deleteInteractive(Long id) {
        // 校验存在
        validateInteractiveExists(id);
        // 删除
        interactiveMapper.deleteById(id);
    }

    private void validateInteractiveExists(Long id) {
        if (interactiveMapper.selectById(id) == null) {
            throw exception(INTERACTIVE_NOT_EXISTS);
        }
    }

    @Override
    public InteractiveDO getInteractive(Long id) {
        return interactiveMapper.selectById(id);
    }

    @Override
    public PageResult<InteractiveDO> getInteractivePage(InteractivePageReqVO pageReqVO) {
        return interactiveMapper.selectPage(pageReqVO);
    }

}